"use strict";
var debugLog = require('../../o2o/common/function.js').debugLog;

var handlerList = [
  editHandler,
  likeHandler,
  postHandler,
  replyHandler,
  statusHandler,
  viewsHandler,
  deleteHandler];
module.exports = function (reqJson, resJson) {
  if (reqJson.method.toLowerCase() === 'option') {
    return {match: false, msg: 'not handle: OPTION method'};
  }

  if (typeof reqJson !== 'object' || typeof resJson !== 'object') {
    return {match: false, msg: 'request/response should be json'};
  }

  for (var handler of handlerList) {
    var result = handler(reqJson, resJson);
    if (result.match) {
      return result;
    }
    if (result.consume) {
      return {match: false, msg: result.msg};
    }
  }
  return {match: false, msg: reqJson.method + ' ' + reqJson.url};
};


var __editHeader = /\/manapi\/v\d\/postEdit/;
var __editMethod = 'post';
function editHandler(req, res) {
  if (!req.url.match(__editHeader) || req.method.toLowerCase() !== __editMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }
  if (req.params.tid === undefined) {
    return {match: false, consume: true, msg: 'tid null @ ' + req.url};
  }

  return {
    match: true,
    succ: res.status == 200,
    logstash: {
      action: 'edit',
      tid: req.params.tid,
      uid: req.loginStatus.uid,
      articleContent: req.params.postCon
    }
  }
}


var __likeHeader = /\/api\/v\d\/like/;
var __likeMethod = 'post';
function likeHandler(req, res) {
  if (!req.url.match(__likeHeader) || req.method.toLowerCase() !== __likeMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }
  if (res.message.level == 1) {// 已经点过赞
    return {match: false, consume: true, msg: 'like, 已经点过赞'};
  }
  return {
    match: true,
    succ: res.status == 200,
    logstash: {
      action: 'like',
      tid: req.params.tid,
      uid: req.loginStatus.uid,
      liketimes: res.result.liketimes
    }
  }
}


var __postHeader = /\/api\/v\d\/thread/;
var __postMethod = 'post';
function postHandler(req, res) {
  if (!req.url.match(__postHeader) || req.method.toLowerCase() !== __postMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }
  let nowTime = new Date().getTime();

  return {
    match: true,
    succ: res.status == 200,
    logstash: {
      action: 'post',
      tid: res.result.tid,
      uid: req.loginStatus.uid,
      vest: +!!req.loginStatus.vestuid,
      articleContent: req.params.post,
      cid: req.params.cid || res.result.cid,
      // default value
      ctype: typeof res.result.ctype == 'undefined' ? 1 : res.result.ctype,
      timeline: nowTime,
      lastpost: nowTime,
      cid2: -1,
      views: 0,
      liketimes: 0,
      status: 1,
      replies: 0,
      userreplies: 0
    }
  }
}


var __replyHeader = /\/api\/v\d\/post/;
var __replyMethod = 'post';
function replyHandler(req, res) {
  // debugLog(' [debug] logAnalyze replyHandler');
  if (!req.url.match(__replyHeader) || req.method.toLowerCase() !== __replyMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }

  return {
    match: true,
    succ: res.status == 200,
    logstash: {
      action: 'reply',
      tid: req.params.tid,
      uid: req.loginStatus.uid,
      vest: +!!req.loginStatus.vestuid
    }
  }
}


var __statusHeader = /\/manapi\/v\d\/manThread/;
var __statusMethod = 'post';
function statusHandler(req, res) {
  // debugLog(' [debug] logAnalyze statusHandler');
  if (!req.url.match(__statusHeader) || req.method.toLowerCase() !== __statusMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }

  // ctype
  if (typeof req.params.ctype !== 'undefined') {
    return {
      match: true,
      succ: res.status == 200,
      logstash: {
        action: 'edit',
        uid: req.loginStatus.uid,
        ctype: res.result.ctype,
        tid: req.params.tid // may have many tid
      }
    }
  }

  // status
  if (typeof req.params.status !== 'undefined') {
    return {
      match: true,
      succ: res.status == 200,
      logstash: {
        action: 'status',
        uid: req.loginStatus.uid,
        status: res.result.status,
        ctype: res.result.ctype,
        tid: res.result.tid // may have many tid
      }
    }
  }
  return {match: false, consume: true, msg: 'not ctype or status @ ' + req.url};
}


var __viewsHeader = /\/api\/v\d\/postlist/;
var __viewsMethod = 'get';
function viewsHandler(req, res) {
  // debugLog(' [debug] logAnalyze viewsHandler');
  if (!req.url.match(__viewsHeader) || req.method.toLowerCase() !== __viewsMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }
  if (req.params.act != 8) { // act =  8
    return {match: false, consume: true, msg: 'act !=8 @ ' + req.url};
  }
  var theThread = res.result.items[0];
  if (theThread.status === 0 // 无效帖子
    || req.params.manager == 1 // 后台查看不计算
  ) {
    return {match: false, consume: true, msg: '无效帖子status=0 or 后台查看不计算manager=1'};
  }
  return {
    match: true,
    succ: res.status == 200,
    logstash: {
      action: 'view',
      uid: req.loginStatus.uid,
      tid: res.result.lastid // may have many tid
    }
  }
}

var __deleteHeader = /\/api\/v\d\/delPost/;
var __deleteMethod = 'post';
function deleteHandler(req, res) {
  // debugLog(' [debug] logAnalyze viewsHandler');
  if (!req.url.match(__deleteHeader) || req.method.toLowerCase() !== __deleteMethod) {
    return {match: false, consume: false, msg: 'url/method not match'};
  }
  if (req.params.act != 1 || req.params.status != 0) {
    return {match: false, consume: true, msg: 'del act!=1 or status !=0 @ ' + req.url};
  }

  return {
    match: true,
    succ: res.status == 200,

    logstash: {
      desc: 'false delete',
      action: 'status',
      uid: req.loginStatus.uid,
      tid: [req.params.tid]
    }
  }
}